// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "waveeditor.h"
#include "mygraphframe.h"
#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNAMIC(ParentMainFrame, CMDIFrameWnd)

BEGIN_MESSAGE_MAP(ParentMainFrame, CMDIFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_WM_DROPFILES()
	//}}AFX_MSG_MAP
	ON_WM_SIZING()
	ON_WM_SIZE()
	ON_COMMAND_EX( ID_VIEW_STATUS_BAR, OnShowHideStatusBar )
	ON_COMMAND( ID_VIEW_DEBUG_OUTPUT_VIEW, OnShowHideDebugView )
	ON_UPDATE_COMMAND_UI( ID_VIEW_DEBUG_OUTPUT_VIEW, OnUpdateViewDebugOutputViewButton )

END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_1,
	ID_INDICATOR_1,
	ID_INDICATOR_1,
	ID_INDICATOR_1,
};
static UINT indicators2[] =
{
	ID_SEPARATOR,
	ID_INDICATOR_1,
	ID_INDICATOR_1,
	//ID_INDICATOR_1, // requires for displaing ToolTip 
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

ParentMainFrame::ParentMainFrame()
{
	// TODO: add member initialization code here
	
}

ParentMainFrame::~ParentMainFrame()
{
}

int ParentMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.Load24BitToolBar( IDR_MAINFRAME, IDB_TOOLBAR_DISABLED_BITMAP ) )
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}
	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT)) ||
		!m_wndStatusBar2.Create(this) ||
		!m_wndStatusBar2.SetIndicators(indicators2, sizeof(indicators2)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}
	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	return 0;
}

BOOL ParentMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CMDIFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	cs.style	|=WS_MAXIMIZE   ;		
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void ParentMainFrame::AssertValid() const
{
	CMDIFrameWnd::AssertValid();
}

void ParentMainFrame::Dump(CDumpContext& dc) const
{
	CMDIFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers



CFrameWnd* ParentMainFrame::OnDebugWindow() 
{
	// TODO: Add your command handler code here
	CMDIChildWnd	*pChild	=	MDIGetActive();
	CDocument		*pD		=	pChild->GetActiveDocument();
	CDocTemplate	*pTempl	=	((ZWaveeditorApp*)AfxGetApp())->pTemplateList;
	CFrameWnd		*pFrame	=	pTempl->CreateNewFrame(pD,pChild);
	pTempl->InitialUpdateFrame(pFrame,pD);
	pD->UpdateAllViews(0);
	return pFrame;
}

CFrameWnd* ParentMainFrame::OnTreeWindow() 
{
	// TODO: Add your command handler code here
	CMDIChildWnd	*pChild	=	MDIGetActive();
	CDocument		*pD		=	pChild->GetActiveDocument();
	CDocTemplate	*pTempl	=	((ZWaveeditorApp*)AfxGetApp())->pTemplateTree;
	CFrameWnd		*pFrame	=	pTempl->CreateNewFrame(pD,NULL);
	pTempl->InitialUpdateFrame(pFrame,pD);
	pD->UpdateAllViews(0);
	return pFrame;
}



CFrameWnd* ParentMainFrame::onEditWindow()
{
	CMDIChildWnd	*pChild	=	MDIGetActive();
	CDocument		*pD		=	pChild->GetActiveDocument();
	CDocTemplate	*pTempl	=	((ZWaveeditorApp*)AfxGetApp())->pTemplateEdit;
	CFrameWnd		*pFrame	=	pTempl->CreateNewFrame(pD,NULL);
	pTempl->InitialUpdateFrame(pFrame,pD);
	pD->UpdateAllViews(0);
	return pFrame;
}



void ParentMainFrame::OnDropFiles(HDROP hDropInfo) 
{
	// TODO: Add your message handler code here and/or call default
	
	CMDIFrameWnd::OnDropFiles(hDropInfo);
	OnTreeWindow();
}


void ParentMainFrame::OnCloseAllEditWindows()
{
	CMDIChildWnd	*pChild	=	MDIGetActive();
	CDocument		*pD		=	pChild->GetActiveDocument();
	CDocTemplate	*pTempl	=	((ZWaveeditorApp*)AfxGetApp())->pTemplateEdit;
//	CFrameWnd		*pFrame	=	pTempl->CreateNewFrame(pD,NULL);
//	pTempl->InitialUpdateFrame(pFrame,pD);
	pTempl->CloseAllDocuments(TRUE);
	pD->UpdateAllViews(0);
}

CFrameWnd*  ParentMainFrame::onGraphWindow()
{
	CMDIChildWnd	*pChild	=	MDIGetActive();
	CDocument		*pD		=	pChild->GetActiveDocument();
	CDocTemplate	*pTempl	=	((ZWaveeditorApp*)AfxGetApp())->pTemplateGraph;
	CFrameWnd		*pFrame	=	pTempl->CreateNewFrame(pD,NULL);
	pTempl->InitialUpdateFrame(pFrame,pD);
	pD->UpdateAllViews(0);
	return pFrame;

}

void ParentMainFrame::OnSizing(UINT fwSide, LPRECT pRect)
{
	CMDIFrameWnd::OnSizing(fwSide, pRect);
//	SendMessageToDescendants(WM_SIZING);
	// TODO: Add your message handler code here
}

void ParentMainFrame::OnSize(UINT nType, int cx, int cy)
{
	CMDIFrameWnd::OnSize(nType, cx, cy);
	SendMessageToDescendants(WM_SIZING);

	// TODO: Add your message handler code here
}
BOOL ParentMainFrame::OnShowHideStatusBar( UINT nID )
{
	ShowControlBar( &m_wndStatusBar2, (m_wndStatusBar2.GetStyle() & WS_VISIBLE) == 0, FALSE);
	return CFrameWnd::OnBarCheck( nID );
}

void ParentMainFrame::OnShowHideDebugView()
{
		if ( pDebugFrame != NULL )
			pDebugFrame->ShowWindow( IsDebugViewVisible()? SW_HIDE : SW_SHOW );
}

void ParentMainFrame::OnUpdateViewDebugOutputViewButton( CCmdUI* pCmdUI )
{
		pCmdUI->SetCheck( IsDebugViewVisible() );
}

bool ParentMainFrame::IsDebugViewVisible()
{
	//ZWaveeditorApp *pWinapp = (ZWaveeditorApp*)AfxGetApp();
	bool Result = false;
	if ( pDebugFrame != NULL )
	{
		WINDOWINFO WI;
		pDebugFrame->GetWindowInfo( &WI );
		Result = WI.dwStyle & WS_VISIBLE;
	}
	return Result;
}
